package com.lzh.financial.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lzh.financial.bean.PUserBean;
import com.lzh.financial.dto.UserDTO;
import com.lzh.financial.service.IUserService;
import com.lzh.financial.util.MyPwdUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author reborn0530
 * @date 2021/2/5 0:13
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    PasswordEncoder passwordEncoder;

    @Resource
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDTO userDTO = userService.selectbyUserName(username);
        //1.不存在会报异常
        if (ObjectUtil.isNull(userDTO)){
            throw new UsernameNotFoundException("用户不存在");
        }
        return new  User(username,userDTO.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }


}
